﻿using System;
using mshtml;

namespace MQuery
{
    /// <summary>
    /// jQuery stryl extension methods to <see cref="IHTMLElement"/>
    /// </summary>
    public static class MQueryExtensions
    {
        /// <summary>
        /// Determines whether the specified element has class.
        /// $(http://docs.jquery.com/Attributes/hasClass)
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="className">Name of the class.</param>
        /// <returns>
        /// 	<c>true</c> if the specified element has the class; otherwise, <c>false</c>.
        /// </returns>
        public static bool HasClass(this IHTMLElement element, string className)
        {
            if (string.IsNullOrEmpty(element.className))
            {
                return false;
            }
            else
            {
                foreach( string classPart in element.className.Split(' ') )
                {
                    if (string.Equals(classPart, className, StringComparison.InvariantCultureIgnoreCase))
                    {
                        return true;
                    }
                }
            }
            return false;           
        }

        /// <summary>
        /// Returns a specific attribute of the specified element.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="name">The attribute's name.</param>
        /// <returns>The attribute's value if exists or null if it does not.</returns>
        public static string Attribute(this IHTMLElement element, string name)
        {
            object att = element.getAttribute(name, 0);
            if (att == DBNull.Value)
            {
                return null;
            }
            
            return (string)att;
        }

        /// <summary>
        /// Returns the text content of the specified element.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns>The text content of the specified element.</returns>
        public static string Text(this IHTMLElement element)
        {
            return element.innerText;
        }

        /// <summary>
        /// Traverses over the specified element's child and perform the callback on each of them.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="callback">The callback.</param>
        public static void Traverse(this IHTMLElement element, Action<IHTMLElement> callback)
        {
            foreach(IHTMLElement childElement in (IHTMLElementCollection)element.children)
            {
                childElement.Traverse(callback);
            }
            callback(element);
        }

        /// <summary>
        /// Return a row's cell by index.
        /// </summary>
        /// <param name="row">The row.</param>
        /// <param name="index">The index.</param>
        /// <returns>A <see cref="HTMLTableCellClass"/> in the specified index</returns>
        public static HTMLTableCellClass Cell(this HTMLTableRowClass row, int index)
        {
            return (HTMLTableCellClass)row.cells.item(index, index);
        }
    } 
}
